package com.doordu.soa.service.web.converter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;

public class String2LocalDateTimeConverter implements Converter<String, LocalDateTime> {
	private static Logger log = LoggerFactory.getLogger(String2LocalDateTimeConverter.class);
	@Override
	public LocalDateTime convert(String source) {
		if (StringUtils.isBlank(source)) {
			return null;
		}

		String pattern = "yyyy-MM-dd HH:mm:ss";
		if (source.indexOf("/") > 0) {
			pattern = "yyyy/MM/dd HH:mm:ss";
		}
		try {
			return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(pattern));
		} catch (Exception ex) {
			log.error("日期参数格式转换错误! source="+source);
			throw new RuntimeException(ex);
		}
	}

}
